﻿using System.Windows;
using System.Windows.Controls;
using Samba.Presentation.Common;
using System.Windows.Input;

namespace Samba.Modules.CreditCardModule.ExternalProcessor
{
    /// <summary>
    /// Interaction logic for ExternalProcessorView.xaml
    /// </summary>

    public partial class ExternalProcessorView : Window
    {
        public ExternalProcessorView(ExternalProcessorViewModel viewModel)
        {
            DataContext = viewModel;
            InitializeComponent();
            setView("Swipe");
        }

        private void ExternalProcessorWindow_Loaded(object sender, RoutedEventArgs e)
        {
            SwipeDataBox.BackgroundFocus();
        }

        public void setView(string _viewType)
        {
            tabControl1.SelectedItem = (TabItem)tabControl1.FindName(_viewType);
            tabControl1.Refresh();
        }
        public void ToggleSwipeManual()
        {
            if (ManualSwipeButton.Content.ToString() == "Manual")
            {
                ManualSwipeButton.Content = "Swipe";
                setView("Manual");
                creditCardBox.Focus();
            }
            else
            {
                ManualSwipeButton.Content = "Manual";
                setView("Swipe");
                SwipeDataBox.Focus();
            }
            
        }

        private void SwipeDataBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Return))
            {
                SwipeDataBox.Password = 
                    (SwipeDataBox.Password.Substring(SwipeDataBox.Password.LastIndexOf("%")));
                //authButton.IsEnabled = true;
                CardDataReceived(sender);
                System.Diagnostics.Debug.WriteLine(SwipeDataBox.Password);
             }
            
        }
        private void CardDataReceived(object sender)
        {
            // Validate Card Data
            // Check Swipe Data
            if (SwipeDataBox.Password[0].ToString().Equals("%") & SwipeDataBox.Password[SwipeDataBox.Password.Length - 1].ToString().Equals("?"))
            {
                authButton.IsEnabled = true;
                statusBox.Text = "Card Data Received.";
            }
        }
        private void AuthButton_Click(object sender, RoutedEventArgs e)
        {

        }


    }
}
